package com.eva.baidulibrary;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiSearch;

/**
 * Class description
 *
 * @author GUXIA318
 * @date 2015-09-06 10:55
 */
public class BDMapClient {

    private static BDMapClient sInstance;
    private PoiSearch mPoiSearch;

    public static BDMapClient getInstance() {
        if (sInstance == null) {
            sInstance = new BDMapClient();
        }
        return sInstance;
    }

    /**
     * 地图缩放，缩放级别范围： [3.0,19.0]
     *
     * @param map
     * @param level
     */
    public void zoom(BaiduMap map, float level) {
        MapStatusUpdate u = MapStatusUpdateFactory.zoomTo(level);
        map.animateMapStatus(u);
    }

    /**
     * 开启定位图层
     *
     * @param map
     * @param resId
     *          定位自定义marker，默认marker传入0
     */
    public void openMyLocation(BaiduMap map, int resId) {
        BitmapDescriptor marker = null;
        if (resId > 0) {
            marker = BitmapDescriptorFactory.fromResource(resId);
        }

        MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, false, marker);
        map.setMyLocationConfigeration(config);
        map.setMyLocationEnabled(true);
    }

    /**
     * 关闭定位图层
     *
     * @param map
     */
    public void closeMyLocation(BaiduMap map) {
        map.setMyLocationEnabled(false);
    }

    /**
     * 显示定位
     *
     * @param map
     * @param location
     */
    public void showMyLocation(BaiduMap map, BDLocation location, float zoomLevel) {
        // 构造定位数据
        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(1000) // 半径
                // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(0).latitude(location.getLatitude())
                .longitude(location.getLongitude()).build();
        // 设置定位数据
        map.setMyLocationData(locData);
        updateMapView(map, new LatLng(location.getLatitude(), location.getLongitude()), zoomLevel);
    }

    /**
     * 更新地图
     *
     * @param map
     * @param ll
     */
    public void updateMapView(BaiduMap map, LatLng ll, float zoomLevel) {
        MapStatus status = new MapStatus.Builder().target(ll).zoom(zoomLevel).build();
        MapStatusUpdate update = MapStatusUpdateFactory.newMapStatus(status);
        map.animateMapStatus(update);
    }

    /**
     * POI周边搜索
     *
     * @param radius
     *          搜索半径
     * @param keyword
     *          搜索关键字
     * @param ll
     *          搜索位置
     * @param listener
     *          搜索结果监听
     */
    public void searchNearby(int radius, String keyword, LatLng ll, OnGetPoiSearchResultListener listener) {
        if (mPoiSearch == null) {
            mPoiSearch = PoiSearch.newInstance();
        }

        mPoiSearch.setOnGetPoiSearchResultListener(listener);
        mPoiSearch.searchNearby((new PoiNearbySearchOption()).keyword(keyword).location(ll).radius(radius));
    }


}
